//
//  EditPassWordViewController.m
//  有材网
//
//  Created by 肖扬 on 16/1/20.
//  Copyright © 2016年 肖扬. All rights reserved.
//

#import "EditPassWordViewController.h"
#import "XlineView.h"
#import "LoginViewController.h"
@interface EditPassWordViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *firstTextField;
@property (nonatomic,strong)UITextField *secondTextField;
@property (nonatomic,strong)UITextField *thirdTextField;
@end

@implementation EditPassWordViewController
@synthesize firstTextField,secondTextField,thirdTextField;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.leftBtn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    [self.rightBtn setTitle:@"完成" forState:0];
    [self.rightBtn setEnabled:NO];
    [self.rightBtn setTitleColor:ButtonGrayColor forState:0];
//    [self.navigationItem.rightBarButtonItem setEnabled:NO];
    [self.rightBtn addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    UIView *tempView = [self createViewFrame:CGRectMake(0, 74, SWIDTH, 129) bgColor:[UIColor whiteColor] tag:20];
    [self.view addSubview:tempView];
    NSArray *arr = [NSArray arrayWithObjects:@"请输入原密码",@"请输入新密码",@"再次输入新密码", nil];
    for (int i = 0; i < [arr count]; i++) {
        UITextField *textField = [self createTextFieldFrame:CGRectMake(10, i*43, SWIDTH - 20, 43) bgColor:[UIColor clearColor] tag:10+i keyBoardType:0];
        textField.placeholder = [arr objectAtIndex:i];
        textField.font = [UIFont systemFontOfSize:14];
        textField.textColor = GRAYColor;
        textField.textAlignment = NSTextAlignmentLeft;
        [textField setSecureTextEntry:YES];
        textField.delegate = self;
        [tempView addSubview:textField];
        if (i==0) {
            [textField becomeFirstResponder];
        }
        if (i != [arr count]-1) {
            XlineView *lineView = [[XlineView alloc]initWithFrame:CGRectMake(0, 42.5, SWIDTH - 0, 0.5)];
            lineView.backgroundColor = [UIColor grayColor];
            lineView.alpha = 0.2;
            [textField addSubview:lineView];
        }
    }
    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
    // Do any additional setup after loading the view.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.text.length == 0) {
        textField.textColor = BLACKColor;
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0) {
        textField.textColor = GRAYColor;
    }
}
-(void)textChange
{
    UIView *tempView = (UIView *)[self.view viewWithTag:20];
    UITextField *field1 = (UITextField *)[tempView viewWithTag:10];
    UITextField *field2 = (UITextField *)[tempView viewWithTag:11];
    UITextField *field3 = (UITextField *)[tempView viewWithTag:12];
    if (field1.text.length !=0 && field2.text.length !=0 && field3.text.length !=0) {
        [self.rightBtn setEnabled:YES];
        [self.rightBtn setTitleColor:WhiteColor forState:0];
    }else{
        [self.rightBtn setEnabled:NO];
        [self.rightBtn setTitleColor:ButtonGrayColor forState:0];
    }
}

-(void)submitClick:(UIButton *)sender{
    UIView *tempView = (UIView *)[self.view viewWithTag:20];
    UITextField *field1 = (UITextField *)[tempView viewWithTag:10];
    UITextField *field2 = (UITextField *)[tempView viewWithTag:11];
    UITextField *field3 = (UITextField *)[tempView viewWithTag:12];
    if ([field1.text isEqualToString:@""]) {
        SHOW_ALERT(@"请输入原密码");
        return;
    }
    if ([field2.text isEqualToString:@""]) {
        SHOW_ALERT(@"请输入新密码");
        return;
    }
    if ([field3.text isEqualToString:@""]) {
        SHOW_ALERT(@"再次输入新密码");
        return;
    }
    if (![field2.text isEqualToString:field3.text]) {
        SHOW_ALERT(@"新密码不一致");
        return;
    }
    TIME
    HEADDIC(nowTime, MODIFYPASSWORD);
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    [body setObject:field1.text forKey:@"oldpassword"];
    [body setObject:field2.text forKey:@"newpassword"];
    NSString *result = [Unity requestSomeApiData:HEAD withBody:body];
    NSMutableDictionary *json = [result objectFromJSONString];
    if ([[json objectForKey:@"err_code"] integerValue]==0) {
        [[NSUserDefaults standardUserDefaults] setObject:field2.text forKey:@"userPwd"];
        [[NSUserDefaults standardUserDefaults] setObject:field2.text forKey:@"newpassword"];
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isLogin"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"certificate"];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[json objectForKey:@"err_message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 255;
        [alert show];
    }else{
        SHOW_ALERT([json objectForKey:@"err_message"]);
    }
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 255) {
//        [self.navigationController popToRootViewControllerAnimated:YES];
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        loginVC.title = @"登录";
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
